package com.hkzhao.shixun.service.impl;

import com.hkzhao.shixun.common.api.CommonResult;
import com.hkzhao.shixun.common.utils.DrawsLotteryUtil;
import com.hkzhao.shixun.common.utils.TokenUtil;
import com.hkzhao.shixun.component.CancleLotteryRecordSender;
import com.hkzhao.shixun.dto.RedisLotteryRecord;
import com.hkzhao.shixun.mbg.mapper.ActivityMapper;
import com.hkzhao.shixun.mbg.mapper.LotteryrecordMapper;
import com.hkzhao.shixun.mbg.mapper.PrizeMapper;
import com.hkzhao.shixun.mbg.model.Activity;
import com.hkzhao.shixun.mbg.model.Lotteryrecord;
import com.hkzhao.shixun.mbg.model.Prize;
import com.hkzhao.shixun.mbg.model.PrizeExample;
import com.hkzhao.shixun.service.DrawsLotteryService;
import com.hkzhao.shixun.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
/**
 * <h3>j2ee</h3>
 * <p>抽奖服务类实现</p>
 *
 * @author : hkzhao
 * @date : 2020-06-06 17:11
 **/

@Service
public class DrawsLotteryServiceImpl implements DrawsLotteryService {
    @Value("${redis.key.LotteryRecordHeaderEveryDay}")
    private String LotteryRecordHeaderByDay;
    @Value("${redis.key.LotteryRecordHeaderTotal}")
    private String LotteryRecordHeaderTotal;
    @Value("${rabbitmq.expire}")
    private Long ExpireTime;
    @Autowired
    private RedisService redisService;
    @Autowired
    private DrawsLotteryUtil drawsLotteryUtil;
    @Autowired(required = false)
    private PrizeMapper prizeMapper;
    @Autowired(required = false)
    private LotteryrecordMapper lotteryrecordMapper;
    @Autowired(required = false)
    private ActivityMapper activityMapper;
    @Autowired
    private CancleLotteryRecordSender cancleLotteryRecordSender;
    @Override
    public CommonResult DrawsLottery(String token_id, String activityId) {
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        String key = null;
        if(activity.getDrawstype().equals("1")){
            key = LotteryRecordHeaderTotal+token_id+activityId;
        }else {
        key = LotteryRecordHeaderByDay+token_id+activityId;}
        RedisLotteryRecord value = (RedisLotteryRecord)redisService.get(key);
        if(value==null||value.getNums()<value.getLimit())
        {   PrizeExample prizeExample = new PrizeExample();
            prizeExample.createCriteria().andActivityidEqualTo(activityId);
            List<Prize> prizes = prizeMapper.selectByExample(prizeExample);
            Prize prize = drawsLotteryUtil.draws(prizes);
            if(prize!=null){
            prize.setHaslotterynum(prize.getHaslotterynum()+1);
            if (value==null)
            {
                value=new RedisLotteryRecord();
                value.setNums(0);
                value.setLimit(activity.getDrawsnum());
            }
            value.setNums(value.getNums()+1);
            //加上消息队列以及把中奖记录放数据库中
            Lotteryrecord lotteryrecord = new Lotteryrecord();
            String RecordId = TokenUtil.GetGUID();
            lotteryrecord.setActivityid(activityId);lotteryrecord.setPrizename(prize.getLevel());
            lotteryrecord.setRecordtime(new Date());lotteryrecord.setRecordid(RecordId);lotteryrecord.setUsername(token_id);
            lotteryrecordMapper.insert(lotteryrecord);
            prizeMapper.updateByPrimaryKey(prize);
            redisService.set(key,value);
            cancleLotteryRecordSender.sendMessage(RecordId,ExpireTime);
            Map<String,Object> mp=new HashMap<>();
            mp.put("remain",value.getLimit()-value.getNums());
            mp.put("level",prize.getLevel());
            return CommonResult.success(mp);
            } else {
                return CommonResult.failed("奖品全部抽光了");
            }
        }
        return CommonResult.failed("没有抽奖次数了");
    }

    @Override
    public void deleteLotteryRecord(String recordId) {
        Lotteryrecord lotteryrecord = lotteryrecordMapper.selectByPrimaryKey(recordId);
        if(lotteryrecord.getLocation().length()!=0){
            return;
        }
        lotteryrecordMapper.deleteByPrimaryKey(recordId);
    }

    @Override
    public void deleteLotteryNumsEveryDay(String header) {
        List<String> list = new ArrayList<>( redisService.getKeysByRegular(header));
        redisService.del(list);
    }
}
